package com.itheima.mobilesafe69.service;

import java.util.Timer;
import java.util.TimerTask;

import com.itheima.mobilesafe69.R;
import com.itheima.mobilesafe69.engine.ProcessInfoProvider;
import com.itheima.mobilesafe69.receiver.MyAppWidgetProvider;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.text.format.Formatter;
import android.util.Log;
import android.widget.RemoteViews;

public class UpdateWidgetService extends Service {
	protected static final String tag = "UpdateWidgetService";
	private Timer mTimer;
	private InnerReceiver innerReceiver;
	@Override
	public void onCreate() {
		//定时更新窗体小部件上进程总数,以及可用内存数,定时器
		updateWidgetTimer();
		super.onCreate();
	}
	private void updateWidgetTimer() {
		startTimer();
		
		//锁屏过后,就需要将任务结束,在开锁的时候,又将任务开启
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
		intentFilter.addAction(Intent.ACTION_SCREEN_ON);
		
		innerReceiver = new InnerReceiver();
		registerReceiver(innerReceiver, intentFilter);
	}
	private void startTimer() {
		//1,创建Timer对象
		mTimer = new Timer();
		//2,调用Timer上定时执行任务的方法(任务,延时时间,执行任务的时间周期)
		mTimer.scheduleAtFixedRate(new TimerTask() {
			@Override
			public void run() {
				Log.i(tag, "正在执行任务...............................");
				updateWidget();
			}
		}, 0, 5000);
	}
	
	class InnerReceiver extends BroadcastReceiver{
		@Override
		public void onReceive(Context context, Intent intent) {
			//判断接受到的广播类型
			if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
				if(mTimer!=null){
					mTimer.cancel();
					mTimer = null;
				}
			}else{
				startTimer();
			}
		}
	}
	/**
	 * 更新widget方法
	 */
	protected void updateWidget() {
		//1,获取widget管理者对象
		AppWidgetManager aWM = AppWidgetManager.getInstance(this);
		//2,获取一个远程的view对象,就是widget对应布局转换成的view对象(应用程序包名,widget用到的布局文件)
		RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.process_widget);
		//3,做查询控件,又赋值(进程总数)
		remoteViews.setTextViewText(R.id.tv_process_count,"进程总数:"+ProcessInfoProvider.getProcessCount(this));
		//4,设置可用内存数
		String formatFileSize = Formatter.formatFileSize(this, ProcessInfoProvider.getAvailSpace(this));
		remoteViews.setTextViewText(R.id.tv_avail_memory,"可用内存总数:"+formatFileSize);
		
		
		//点击小窗体对应的view对象,让其跳转到应用程序主界面中(点中控件id,延时意图要去开启activity所以调用getActivity)
		Intent intent = new Intent("com.itheima.mobilesafe69");
		intent.addCategory("android.intent.category.DEFAULT");
		//延时intent
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		//在响应了ll_root控件的点击事件后,就去开启此延时意图
		remoteViews.setOnClickPendingIntent(R.id.ll_root, pendingIntent);
		
		//点击一键清理后,进程总数减少,可用内存数增加,发送广播,让指定广播接受者接收此广播,然后调用杀死进程的方法
		Intent broadcastIntent = new Intent("com.itheima.mobilesafe69.KILL_PROCESS");
		PendingIntent broadcastPendingIntent = PendingIntent.getBroadcast(this,0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		
		remoteViews.setOnClickPendingIntent(R.id.bt_clear, broadcastPendingIntent);
		
		//5,让widget管理者对象,做widget更新
		//widget中广播接受者的字节码
		ComponentName componentName = new ComponentName(this, MyAppWidgetProvider.class);
		aWM.updateAppWidget(componentName, remoteViews);
		
	}
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	@Override
	public void onDestroy() {
		//如果定时器不为空,取消此定时器
		if(mTimer!=null){
			mTimer.cancel();
			mTimer = null;
		}
		
		if(innerReceiver!=null){
			unregisterReceiver(innerReceiver);
		}
		super.onDestroy();
	}
}
